package dk.ahp.getmeoff;

import java.io.IOException;
import java.util.List;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.util.Log;

/**
 * En Destination består af en destiantion og muligvis et start sted.
 */
public class Destination {
	private static final String TAG = Destination.class.getSimpleName();
	private long id=0;
	private String name;
	private Location startPoint;
	private Location destPoint;
	private String startAddress;
	private String destAddress;

	/**
	 * 
	 * @param name Name for this destination.
	 * @param destination The location the user needs to get off bus or train.
	 * @param startPoint Optional starting point for this route. 
	 * @param id Row number from db.
	 */
	Destination(String name, Location destination, Location startPoint, long id, String startAddress, String destAddress) {
		this.name = name;
		this.id = id;
		this.destPoint = destination;
		this.startPoint = startPoint;
		this.startAddress = startAddress;
		this.destAddress = destAddress;
	}

	public long getId() {
		return id;
	}
	
	/**
	 * @param id Sætter id hvis ikke id er sat.
	 */
	public void setId(long id) {
		if(0 != this.id) {
			this.id = id;
		}
	}
	
	public String getStartAddress() {
		return startAddress;
	}
	
	public String getDestAddress() {
		return destAddress;
	}
	
	public void setStartAddress(String sAddress) {
		startAddress = sAddress;
	}
	
	public void setDestAddress(String dAddress) {
		destAddress = dAddress;
	}

	public String getName() {
		return name;
	}
	
	public void setName(String n) {
		this.name = n;
	}

	public Location getStart(){
		return startPoint;
	}
	
	public Location getDestination() {
		return destPoint;
	}
	
	public void setStart(Location l) {
		startPoint = l;
		startAddress = null;
	}
	
	public void setDestination(Location l) {
		destPoint = l;
		destAddress = null;
	}

	public String toString() {
		StringBuilder string = new StringBuilder();
		string.append("Name:\t");
		string.append(getName());
		String tmp = locationToAddres(startPoint, true);
		if (null != tmp) {
			string.append("\nStart sted:\n" + tmp);
		}
		tmp = locationToAddres(destPoint, true);
		if (null != tmp) {
			string.append("\nBestemmelsested sted:\n" + tmp);
		}
		return string.toString();
	}
	
	/**
	 * @param s Location to find an address for.
	 * @param returnLatLonStr If true lat/lon will be in the string returned if an adderess can not be found.
	 * @return Address or lat/lan in a string.
	 */
	public static String locationToAddres(Location s, boolean returnLatLonStr) {
		String res = new String();
		if (null != s) {
			try {
				Geocoder geocoder = new Geocoder(GetMeOffApplication.getMeOffApplication);
				List<Address> adresser = geocoder.getFromLocation(s.getLatitude(), s.getLongitude(), 1);
				if (adresser != null && adresser.size() > 0) {
					Address a = adresser.get(0);
					res += a.getThoroughfare() + " " + a.getFeatureName() + "\n";
					res += a.getPostalCode() + " " + a.getLocality();
				}
			} catch (IOException ex) {
				Log.d(TAG, "Øv Geocoder er ikke med os");
				ex.printStackTrace();
				res = null;
				if (returnLatLonStr) {
					res = s.getLatitude() + " " + s.getLongitude();
				}
			}
		}
		return res;
	}
}
